package ThreadPoolDemo.Exchanger;

import java.util.concurrent.Exchanger;

/**
 * Created by zzy on 2018/4/9.
 */
public class FIllAndEmpty {
    static Exchanger<Date> exchanger=new Exchanger<>();
    static Date  fullDate;
    static Date EmptuDate;
    static Date EmptuDate2;


    public FIllAndEmpty(){
        fullDate=new Date(1,"the one");
        EmptuDate=new Date();
        EmptuDate2=new Date();
    }

    class FillingLoop implements Runnable{
        Date currentBuffer  =FIllAndEmpty.EmptuDate;
        @Override
        public void run() {
           while (currentBuffer.getId()==0){
               try {
                   System.out.println("A:"+currentBuffer);
                   currentBuffer=exchanger.exchange(currentBuffer);
                   if(currentBuffer.getId()!=0){
                       System.out.println("A:"+currentBuffer);
                   }
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        }
    }

    class FillingLoop2 implements Runnable{
        Date currentBuffer  =FIllAndEmpty.EmptuDate2;
        @Override
        public void run() {
            while (currentBuffer.getId()==0){
                try {
                    System.out.println("C:"+currentBuffer);
                    currentBuffer=exchanger.exchange(currentBuffer);//交换不到会进入阻塞状态
                    if(currentBuffer.getId()!=0){
                        System.out.println("C:"+currentBuffer);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class EmptyingLoop  implements Runnable{
        Date currentBuffer  =FIllAndEmpty.fullDate;
        @Override
        public void run() {
            while (currentBuffer.getId()!=0){
                try {
                    System.out.println("B:"+currentBuffer);
                    currentBuffer=exchanger.exchange(currentBuffer);
                    System.out.println("B:"+currentBuffer);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public FillingLoop getFillingLoop(){
        return new FillingLoop();
    }

    public EmptyingLoop getEmptyingLoop(){
        return new EmptyingLoop();
    }

    public FillingLoop2 getFillingLoop2(){
        return new FillingLoop2();
    }

    /**
     * 数据
     */
    class Date{
        private int id;

        private String name;

        Date(int id,String name){
            this.id=id;
            this.name=name;
        }
        Date(){

        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "Date{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        FIllAndEmpty fIllAndEmpty=new FIllAndEmpty();
        new Thread(fIllAndEmpty.getEmptyingLoop()).start();
        new Thread(fIllAndEmpty.getFillingLoop()).start();
        new Thread(fIllAndEmpty.getFillingLoop2()).start();


    }
}
